
//
// 120205 - AH - (Bien) Created.
// 120321 - AH - Removed @Override for interface method (1.5 compatible).
//

package org.soekrat.gui.swing.motor.passingstrategy;

import static org.soekrat.gui.swing.motor.passingstrategy.PassingStrategyUtil.getOntologyClassForCname;
import static org.soekrat.gui.swing.motor.passingstrategy.PassingStrategyUtil.getOntologyClassNameForUname;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.motor.passingstrategies.CheckForContentPassingStrategy;

/**
 * <p>
 *   Panel to specify a
 *   {@link CheckForContentPassingStrategy}.
 * </p>
 * @author Laetitia Le Chatton (LLC) 
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class CheckForContentPassingStrategyPanel extends JPanel 
  implements SequenceReceiver 
{
  private final Soekrat2Engine m_engine;
  
  private final JTextField m_ontologies=new JTextField(30);
  
  private CheckForContentPassingStrategy m_strategy=
     new CheckForContentPassingStrategy();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param se {@link Soekrat2Engine} interfaced.
   */
  public CheckForContentPassingStrategyPanel(Soekrat2Engine se)
  {
    if (se==null)
    {
      throw new NullPointerException("se");
    }
    
    m_engine=se;
    
    setLayout(new BorderLayout());
    
    JPanel center=new JPanel(new FlowLayout(FlowLayout.LEFT));
    
    center.add(new JLabel(" Force "));
    center.add(m_ontologies);
    
    add(center, BorderLayout.CENTER);
    
    JPanel south=new JPanel(new FlowLayout(FlowLayout.LEFT));
    south.add(new JLabel("Ontology: "));
    
    init();
  }
  
  @Override
  public void setEnabled(boolean v){ m_ontologies.setEnabled(v); }
  
  private CheckForContentPassingStrategy getPassingStrategy()
  {
    CheckForContentPassingStrategy ps=
      (CheckForContentPassingStrategy)m_engine.getPassingStrategy(
        CheckForContentPassingStrategy.class);
      
    if (ps==null)
    {
      ps=new CheckForContentPassingStrategy();
      m_engine.addPassingStrategy(ps);
    }
    
    return ps;
  }
  
  private void init()
  {   
    m_ontologies.setText("");
    
    CheckForContentPassingStrategy ps=getPassingStrategy();
     
    String[] names=ps.getSeguence();
      
    int n=names.length;
    StringBuilder sb=new StringBuilder();
    for (int i=0; i<n; i++)
    {
      if (i>0) sb.append(", ");
      String name=names[i];
      String uname=getOntologyClassForCname(name);
      sb.append(uname==null ? name : uname);
    }
    m_ontologies.setText(sb.toString());
  }
  
  private void set()
  {
    CheckForContentPassingStrategy ps=getPassingStrategy();
    
    List<String> seq=new ArrayList<String>();
    
    String txt=m_ontologies.getText();   
    
    StringTokenizer st=new StringTokenizer(txt, ",");
    
    while (st.hasMoreTokens())
    {
      String uname=st.nextToken();
      
      String cname=getOntologyClassNameForUname(uname);
      if (cname!=null)
      {
        seq.add(cname);
      }
    }
    
    ps.setSequence(seq);
    
    m_ontologies.setText("");
    
    m_engine.getMotor().setPassingStrategy(ps);
    
    init();
  }
  
  public String getSequence(){ return m_ontologies.getText(); }

  public void sequence(String txt){ m_ontologies.setText(txt); set(); }
  
}
